package db

import (
	"errors"
	"log"
	"server_cluster/battle/sharemem"
	"strconv"
	"strings"
)

type Config_skill_effect struct {
	Id         int32  `gorm:"primary_key;column:Id;autoIncrement:false" csv:"Id"`
	Target     int8   `gorm:"column:target" csv:"Target,omitempty"`
	Ctype      int8   `gorm:"column:ctype" csv:"ConditionType,omitempty"`
	Condition  string `gorm:"column:condition" csv:"Condition,omitempty"`
	Tag        int32  `gorm:"column:Tag" csv:"EffectTag,omitempty"`
	Begin_time int32  `gorm:"column:Begin_time" csv:"BeginTime,omitempty"`
	End_time   int32  `gorm:"column:End_time" csv:"EndTime"`
	Param1     string `gorm:"column:Param1" csv:"Param1"`
	Param2     string `gorm:"column:Param2" csv:"Param2,omitempty"`
	Param3     int32  `gorm:"column:Param3" csv:"Param3,omitempty"`
	Param4     int32  `gorm:"column:Param4" csv:"Param4,omitempty"`
	Param5     int32  `gorm:"column:Param5" csv:"Param5,omitempty"`
	Param6     string `gorm:"column:Param6" csv:"Param6,omitempty"`
}

func (tmp *Config_skill_effect) TableName() string {
	return "config_skill_effect"
}

func (tmp *Config_skill_effect) getAll() ([]*Config_skill_effect, error) {
	var data []*Config_skill_effect
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_skill_effect) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Sklef)
	for key, value := range data {
		if key >= sharemem.SKILL_EFFECT_MAX {
			return errors.New("config_skill_effect format error 8")
		}
		myshm[key].Id = value.Id
		myshm[key].Target = value.Target
		if value.Ctype == 1 {
			myshm[key].Ctype = false
		}
		if len(value.Condition) > 0 {
			a_list := strings.Split(value.Condition, ";")
			if len(a_list) > sharemem.BATTLE_PARAM_MAX1 {
				return errors.New("config_skill_effect format error 0")
			}
			for k, v := range a_list {
				b_list := strings.Split(v, "|")
				if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
					return errors.New("config_skill_effect format error 00")
				}
				for k1, v1 := range b_list {
					arg, err := strconv.Atoi(v1)
					if err != nil {
						return errors.New("config_skill_effect format error 000")
					}
					myshm[key].Condition[k][k1] = int32(arg)
				}
			}
		}
		myshm[key].Tag = value.Tag
		myshm[key].Begin_time = value.Begin_time
		myshm[key].End_time = value.End_time
		if len(value.Param1) > 0 {
			aa_list := strings.Split(value.Param1, ";")
			if len(aa_list) > sharemem.BATTLE_PARAM_MAX1 {
				return errors.New("config_skill_effect format error 1")
			}
			for k, v := range aa_list {
				b_list := strings.Split(v, "|")
				if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
					return errors.New("config_skill_effect format error 11")
				}
				for k1, v1 := range b_list {
					arg, err := strconv.Atoi(v1)
					if err != nil {
						return errors.New("config_skill_effect format error 111")
					}
					myshm[key].Param1[k][k1] = int32(arg)
				}
			}
		}
		if len(value.Param2) > 0 {
			a_list := strings.Split(value.Param2, ";")
			if len(a_list) > sharemem.BATTLE_PARAM_MAX1 {
				return errors.New("config_skill_effect format error 2")
			}
			for k, v := range a_list {
				b_list := strings.Split(v, "|")
				if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
					return errors.New("config_skill_effect format error 22")
				}
				for k1, v1 := range b_list {
					arg, err := strconv.Atoi(v1)
					if err != nil {
						return errors.New("config_skill_effect format error 222")
					}
					myshm[key].Param2[k][k1] = int32(arg)
				}
			}
		}
		//
		myshm[key].Param3 = value.Param3
		myshm[key].Param4 = value.Param4
		myshm[key].Param5 = value.Param5
		if len(value.Param6) > 0 {
			a_list := strings.Split(value.Param6, ";")
			if len(a_list) > sharemem.BATTLE_PARAM_MAX1 {
				return errors.New("config_skill_effect format error 3")
			}
			for k, v := range a_list {
				arg, err := strconv.Atoi(v)
				if err != nil {
					return errors.New("config_skill_effect format error 33")
				}
				myshm[key].Param6[k] = int32(arg)
			}
		}
		//
	}

	log.Println("load config_skill_effect completed.")

	return nil
}
